/**
 * init
 */
var available = Number($("#availableVal").val());
var advance = Number($("#advanceVal").val());
var min = Number($("#minVal").val());
var max = Number($("#maxVal").val());
var cost = Number($("#costVal").val());
var rate = Number($("#rateVal").val());
var type = 0;


/**
 * Event
 */
$("#cardBankList").change(function () {
    $("#cardBankList option:selected").each(function () {
        $("#cardBank").val($(this).attr("bank"));
        $("#cardSubBank").val($(this).attr("subBank"));
        $("#cardNo").val($(this).attr("no"));
        $("#name").val($(this).attr("name"));
        $("#mobile").val($(this).attr("mobile"));
    });
});

$("#operation_tab li").click(function () {
    $("#operation").val($(this).attr("key"));
    $("#operationName").val($(this).find("a").text());

    type = $($(this).find("a").attr("href")).find(".type option:selected").attr("type");
    $("#type").val(type);
    initAmount();
});

$("#withdraw_type, #transfer_type").change(function () {

    type = $(this).find("option:selected").attr("type");
    $("#type").val(type);

    initAmount();
});

/**
 * Function
 */
function initAmount() {

    // check
    if (available < min && advance < min) {
        $("#amount").attr("readonly", true);
        $("#btnSubmit").attr("disabled", "true");
        return;
    }

    // init
    var amount = $("#amount").val();
    amount = (undefined == amount || null == amount || 0 == amount) ? min : amount;
    // var fee = calcFee(max);
    //+ fee
    var curMax = max;
    var money = 0 == type ? available : advance;
    curMax = curMax < money ? curMax : money;
    amount = amount < min ? min : (amount > curMax ? curMax : amount);

    // fill
    $("#amount").val(amount);
    fee = calcFee(amount);
    $("#fee").val(fee);
    $("#actualAmount").val(amount - fee);
}

function calc() {

    // init
    var amount = $("#amount").val();
    var fee = calcFee(amount);
    var actualAmount = 0;

    // fill
    $("#fee").val(fee);
    $("#actualAmount").val(amount - fee);
}

function calcFee(amount) {

    switch (type) {
        case "1":
        case 1:
            var result = amount * rate;
            return result < 1 ? 1 : result;
        default:
            return cost;
            break;
    }
}

